package com.study.web.controller;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Author zhangfd
 * @Date 2020/3/14 10:15
 * @Version 1.0
 * @Describe：
 */
public class MyHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

      Map<String, String[]> parameterMap = request.getParameterMap();
      String username =  (String)request.getSession().getAttribute("username");
      String password =  (String)request.getSession().getAttribute("password");

      if(parameterMap.size()>0&& null != parameterMap.get("username")){
          username = parameterMap.get("username")[0];
          request.getSession().setAttribute("username",username);
      }

      if(null == username){
          response.sendRedirect("/");
          //request.getRequestDispatcher("/WEB-INF/index.html").forward(request,response);
          return  false;
      }

       if(parameterMap.size()>0 && null != parameterMap.get("password")){
           password = parameterMap.get("password")[0];
           request.getSession().setAttribute("password",password);
       }

       if(null == password ){
           response.sendRedirect("/");
           return  false;
           //request.getRequestDispatcher("/WEB-INF/index.html").forward(request,response);
       }

       String sss = "admin";

      if(!sss.equals(username)|| !sss.equals(password)){
          response.getWriter().write("you donot have security!");
          return false;
      }

        return  true;
    }



}
